package com.catering.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.dao.CateringSpacesChildDao;
import com.catering.entity.CateringSpacesChild;
import com.catering.service.CateringSpacesChildService;
import com.catering.until.CommonUtils;
import com.catering.until.ResultData;
import com.catering.until.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CateringSpacesChildServiceImpl extends ServiceImpl<CateringSpacesChildDao, CateringSpacesChild> implements CateringSpacesChildService {
	
	@Autowired
	private CateringSpacesChildDao cateringSpacesChildDao;

	@Override
	public ResultData select(Map<String,String> map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"t1");
			wrapper.orderByDesc("t1.parent_id");
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringSpacesChild> page =new Page<CateringSpacesChild>(Integer.valueOf(map.get("page").toString()),Integer.valueOf(map.get("limit").toString()));
				IPage<CateringSpacesChild> pageList = cateringSpacesChildDao.selectPageList(page, wrapper);
				Integer count = cateringSpacesChildDao.selectPageCount(wrapper);
				List<CateringSpacesChild> list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				List<CateringSpacesChild> list = cateringSpacesChildDao.selectNoPageList(wrapper);
				return ResultData.success(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SELECT_ERROR.getCode(),ResultEnum.SELECT_ERROR.getMsg());
	}

	@Override
	public ResultData insert(Map<String,String> map) {
		try {
			CateringSpacesChild vo = JSON.parseObject(JSON.toJSONString(map),CateringSpacesChild.class);
			if(CommonUtils.checkNoNull(vo.getSpacesName())) {
				CateringSpacesChild bt = cateringSpacesChildDao.selectOne(new LambdaQueryWrapper<CateringSpacesChild>().eq(CateringSpacesChild::getSpacesName, vo.getSpacesName()).eq(CateringSpacesChild::getParentId, vo.getParentId()));
				if(bt != null) return ResultData.error(ResultEnum.SAME_DATA.getCode(),"规格名称“"+vo.getSpacesName()+"”"+ResultEnum.SAME_DATA.getMsg());
			}else return ResultData.error(ResultEnum.REQ_DATA_ERROR.getCode(),"规格名称"+ResultEnum.REQ_DATA_ERROR.getMsg());
			vo.setCreateTime(CommonUtils.getTime(19));
			cateringSpacesChildDao.insert(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
	}

	@Override
	public ResultData update(Map<String,String> map) {
		try {
			CateringSpacesChild vo = JSON.parseObject(JSON.toJSONString(map),CateringSpacesChild.class);
			if(CommonUtils.checkNoNull(vo.getSpacesName())) {
				CateringSpacesChild bt = cateringSpacesChildDao.selectOne(new LambdaQueryWrapper<CateringSpacesChild>().eq(CateringSpacesChild::getSpacesName, vo.getSpacesName()).eq(CateringSpacesChild::getParentId, vo.getParentId()));
				if(bt != null && !bt.getSysId().equals(vo.getSysId())) return ResultData.error(ResultEnum.SAME_DATA.getCode(),"规格名称“"+vo.getSpacesName()+"”"+ResultEnum.SAME_DATA.getMsg());
			}
			CateringSpacesChild as = cateringSpacesChildDao.selectById(vo.getSysId());
			if(as == null) return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
			cateringSpacesChildDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}

	@Override
	public ResultData delete(Map<String,Object> map) {
		try {
			CateringSpacesChild vo = JSON.parseObject(JSON.toJSONString(map), CateringSpacesChild.class);
			List<String> list = Stream.of(vo.getSysId().split(",")).collect(Collectors.toList());
			cateringSpacesChildDao.deleteBatchIds(list);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
	}
}
